package com.hq.mes.bussiness;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;

import org.springframework.data.jpa.repository.support.SimpleJpaRepository;
import org.springframework.stereotype.Component;

import com.google.common.base.CaseFormat;
import com.hq.mes.bussiness.product.service.dto.ProductQueryCriteria;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import me.zhengjie.annotation.DataPermission;
import me.zhengjie.annotation.Query;
import me.zhengjie.utils.QueryHelp;
import me.zhengjie.utils.SecurityUtils;
import me.zhengjie.utils.StringUtils;
import javax.persistence.Table;

@Component
public class DistinctLikeRepository {

	@PersistenceContext // 注入的是实体管理器,执行持久化操作
	EntityManager entityManager;
	SimpleJpaRepository a;

	public List<String> selectDistinctLike(BussinessBaseQueryCriteria queryCriteria) {

		String tableName =  queryCriteria.getTableName();
		if (null == tableName || tableName.length() == 0) {
			return null;
		}

		Map<String, String> qc = queryCriteria.getAllStringPropertiesNotEmpty();
		if (qc != null && qc.containsKey("distinctColumnName")) {
			String distinctColumnName = qc.get("distinctColumnName");
			
			distinctColumnName=	CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, distinctColumnName);
			
			String sql = "select distinct " + distinctColumnName + " from " + tableName;
			qc.remove("distinctColumnName");
			qc.remove("tableName");
			if (qc.size() > 0) {
				sql += " where 1=1 ";
				for (Map.Entry<String, String> entry : qc.entrySet()) {
					String key=entry.getKey();
					key=CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, key);
					sql += " and "+ key + " like '%" + entry.getValue() + "%' ";
				}
			}
			sql += " limit 10";
			System.out.println("%%%%%%%%%%%%%%%%  "+sql);
			javax.persistence.Query query = entityManager.createNativeQuery(sql);
			List<String> list = query.getResultList();
			
			System.out.println("%%%%%%%%%%%%%%%%  "+list.size());
			
			return list;

		}
		return null;

	}

}
